package com.itfreer.gis.map.style.transfer;

import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

class MbGeoLayer {
    /**
     * 获取wms图层的source对象
     */
    static Map<String, Object> WMS(Map<String, Object> source) {
        int tileSize = source.get("tileSize") == null ? 256 : (int)source.get("tileSize");
        int maxzoom = source.get("maxzoom") == null ? 22 : (int)source.get("maxzoom");
        int minzoom = source.get("minzoom") == null ? 0 : (int)source.get("minzoom");
        String scheme = source.get("scheme") == null ? "xyz" : (String)source.get("scheme");
        boolean urlComlete = source.get("urlComlete") == null ? false : (boolean)source.get("urlComlete");
        String layerName = source.get("layerName") == null ? "" : source.get("layerName").toString();
        String url = source.get("url").toString();
        url = urlComlete ? url : url + "?" // Geoserver WMS地图服务地址
                + "version=1.3.0" //可选1.1.0、1.1.1
                + "&request=GetMap" // 调用的方法名称，获取地图必须是这个方法
                //+ "&BGCOLOR=ff00ff" // 生成的图片背景颜色
                + "&layers=" + layerName // 要显示的图层名称，多个图层用","隔开； 例:layers=itfreer:city,itfreer:lake
                + "&styles=" // 图层显示样式，同样多个样式名称间用，隔开；
                + "&crs=EPSG:3857"  // srs=EPSG:3857 也可以
                + "&bbox={bbox-epsg-3857}" // 使用map加载的wms的时候，使用这个标识来同步要获取地图的范围的坐标，必须为这个值
                + "&width=" + tileSize // 返回的图片的大小
                + "&height=" + tileSize// 返回的图片的大小
                + "&format=image/png" // 返回的图片格式
                + "&TRANSPARENT=TRUE"; // 设置背景是不是可以透明，没有数据的地方就进行透明
        // + "&SLD=http://120.77.2.35:8090/lw_base_mapbox.sld"//客户端可以自定义各个图层显示的样式，该文件定义各个图层显示样式

        LinkedHashMap<String, Object> mbSource = new LinkedHashMap<>();
        mbSource.put("type", "raster");
        mbSource.put("tileSize", tileSize);
        mbSource.put("tiles", new String[] { url });
        mbSource.put("maxzoom", maxzoom);
        mbSource.put("minzoom", minzoom);
        mbSource.put("scheme", scheme);
        return mbSource;
    }

    /**
     * 获取WMTS图层的source组装对象
     */
    static Map<String, Object> WMTS(Map<String, Object> source) {
        int tileSize = source.get("tileSize") == null ? 256 : (int)source.get("tileSize");
        int maxzoom = source.get("maxzoom") == null ? 22 : (int)source.get("maxzoom");
        int minzoom = source.get("minzoom") == null ? 0 : (int)source.get("minzoom");
        String scheme = source.get("scheme") == null ? "xyz" : (String)source.get("scheme");
        boolean urlComlete = source.get("urlComlete") == null ? false : (boolean)source.get("urlComlete");
        String layerName = source.get("layerName") == null ? "" : source.get("layerName").toString();
        String url = source.get("url").toString();
        url = urlComlete ? url : url + "?REQUEST=GetTile&SERVICE=WMTS&VERSION=1.0.0&LAYER=" + layerName +
                "&STYLE=&TILEMATRIX=EPSG:900913:{z}&TILEMATRIXSET=EPSG:900913&FORMAT=image/png&TILECOL={x}&TILEROW={y}";

        LinkedHashMap<String, Object> mbSource = new LinkedHashMap<>();
        mbSource.put("type", "raster");
        mbSource.put("tileSize", tileSize);
        mbSource.put("tiles", new String[] { url });
        mbSource.put("maxzoom", maxzoom);
        mbSource.put("minzoom", minzoom);
        mbSource.put("scheme", scheme);
        return mbSource;
    }

    /**
     * 获取TMS图层
     */
    static Map<String, Object> TMS(Map<String, Object> source) {
        boolean urlComlete = source.get("urlComlete") == null ? false : (boolean)source.get("urlComlete");
        String layerName = source.get("layerName") == null ? "" : source.get("layerName").toString();
        // 地址处理
        String[] tiles = null;
        String last = urlComlete ? "" : "/1.0.0/" + layerName + "@EPSG:900913@pbf/{z}/{x}/{y}.pbf";
        if(source.get("url") != null) {
        	String url = source.get("url").toString() + last;
        	tiles = new String[] { url };
        }else if(source.get("tiles") != null){
        	@SuppressWarnings("unchecked")
			List<String> tilelist = (List<String>) source.get("tiles");
        	tiles = new String[tilelist.size()];
        	for (int i =0;i<tilelist.size();i++) {
        		tiles[i] = tilelist.get(i) + last;
        	}
        }
        int maxzoom = source.get("maxzoom") == null ? 22 : (int)source.get("maxzoom");
        int minzoom = source.get("minzoom") == null ? 0 : (int)source.get("minzoom");
        String scheme = source.get("scheme") == null ? "tms" : (String)source.get("scheme");

        LinkedHashMap<String, Object> mbSource = new LinkedHashMap<>();
        mbSource.put("type", "vector");
        mbSource.put("tiles", tiles);
        mbSource.put("maxzoom", maxzoom);
        mbSource.put("minzoom", minzoom);
        mbSource.put("scheme", scheme);
        return mbSource;
    }

    /**
     * 获取wfs图层
     */
    static Map<String, Object> WFS(Map<String, Object> source) {
        boolean urlComlete = source.get("urlComlete") == null ? false : (boolean)source.get("urlComlete");
        String srsName = source.get("projection") == null ? "EPSG:4326" : source.get("projection").toString();
        String layerName = source.get("layerName") == null ? "" : source.get("layerName").toString();
        String url = source.get("url").toString();
        url = urlComlete ? url : url + "?service=WFS&version=1.0.0&request=GetFeature&typeName=" + layerName +
                "&outputFormat=application/json&srsName=" + srsName;
        int maxzoom = source.get("maxzoom") == null ? 22 : (int)source.get("maxzoom");
        LinkedHashMap<String, Object> mbSource = new LinkedHashMap<>();
        mbSource.put("type", "geojson");
        mbSource.put("data", url);
        mbSource.put("maxzoom", maxzoom);
        return mbSource;
    }
}
